package com.letian.web.controller.record;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.record.domain.RecordAsset;
import com.letian.record.service.IRecordAssetService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 记录资产Controller
 * 
 * @author bmz
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/record/asset")
public class RecordAssetController extends BaseController
{
    @Autowired
    private IRecordAssetService recordAssetService;

    /**
     * 查询记录资产列表
     */
    @PreAuthorize("@ss.hasPermi('record:asset:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordAsset recordAsset)
    {
        startPage();
        List<RecordAsset> list = recordAssetService.selectRecordAssetList(recordAsset);
        return getDataTable(list);
    }

    /**
     * 查询记录资产列表
     */
    @PreAuthorize("@ss.hasPermi('record:asset:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(RecordAsset recordAsset)
    {
        startPage();
        recordAsset.setOwner(getUsername());
        List<RecordAsset> list = recordAssetService.selectRecordAssetList(recordAsset);
        return getDataTable(list);
    }

    /**
     * 导出记录资产列表
     */
    @PreAuthorize("@ss.hasPermi('record:asset:export')")
    @Log(title = "记录资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordAsset recordAsset)
    {
        List<RecordAsset> list = recordAssetService.selectRecordAssetList(recordAsset);
        ExcelUtil<RecordAsset> util = new ExcelUtil<RecordAsset>(RecordAsset.class);
        util.exportExcel(response, list, "记录资产数据");
    }

    /**
     * 获取记录资产详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:asset:query')")
    @GetMapping(value = "/{assetId}")
    public AjaxResult getInfo(@PathVariable("assetId") Long assetId)
    {
        return success(recordAssetService.selectRecordAssetByAssetId(assetId));
    }

    /**
     * 新增记录资产
     */
    @PreAuthorize("@ss.hasPermi('record:asset:add')")
    @Log(title = "记录资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordAsset recordAsset)
    {
        recordAsset.setOwner(getUsername());
        recordAsset.setCreateBy(getUsername());
        return toAjax(recordAssetService.insertRecordAsset(recordAsset));
    }

    /**
     * 修改记录资产
     */
    @PreAuthorize("@ss.hasPermi('record:asset:edit')")
    @Log(title = "记录资产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordAsset recordAsset)
    {
        recordAsset.setUpdateBy(getUsername());
        Long count = recordAsset.getUpdateCount() == null ? 1 : recordAsset.getUpdateCount() + 1;
        recordAsset.setUpdateCount(count);
        return toAjax(recordAssetService.updateRecordAsset(recordAsset));
    }

    /**
     * 删除记录资产
     */
    @PreAuthorize("@ss.hasPermi('record:asset:remove')")
    @Log(title = "记录资产", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assetIds}")
    public AjaxResult remove(@PathVariable Long[] assetIds)
    {
        return toAjax(recordAssetService.deleteRecordAssetByAssetIds(assetIds));
    }
}
